<?php
/**
 * Created by PhpStorm.
 * User: HarrisHuang
 * Date: 17/6/1
 * Time: 19:07
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\ImageText;
use App\Models\ImageTextGroup;
use Log;

class ImageTextController extends Controller
{

    public function save(Request $request, ImageText $imageText)
    {
        $imageText->title = $request->input('title');
        $imageText->picture = $request->input('picture');
        $imageText->content = $request->input('content');
        $imageText->addTime = time();
        $imageText->save();
        return $this->success();
    }


    public function update(Request $request)
    {
        $id = $request->input('id');
        $imageText = ImageText::find($id);
        if (!$imageText) {
            return $this->error();
        }
        $imageText->title = $request->input('title');
        $imageText->picture = $request->input('picture');
        $imageText->content = $request->input('content');
        $imageText->save();
        return $this->success();
    }

    public function lists(Request $request, ImageText $imageText)
    {
        $pageSize = $request->get('pageSize', 10);
        $gid = (int)$request->get('gid', 0);

        $list = $imageText->where('gid',$gid)->orderBy('id', 'desc')->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }


    public function mobileLists(Request $request, ImageText $imageText)
    {
        $pageSize = $request->get('pageSize', 10);
        $gid = (int)$request->get('gid', 0);
        if($gid == 0 ){
            return $this->error();
        }

        $list = $imageText->where('gid',$gid)->select(['id','picture','id','title'])->orderBy('id', 'desc')->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }


    public function info(Request $request)
    {
        $id = (int)$request->get('id');
        $res = ImageText::find($id);
        return $this->success($res);
    }

    public function mobileInfo(Request $request)
    {
        $id = (int)$request->get('id');
        $res = ImageText::find($id);
        return $this->success($res);
    }


    public function delete(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id <= 0) {
            return $this->error();
        }
        ImageText::destroy($id);
        return $this->success();
    }

    /**
     * 修改图文分组
     */
    public function updImgTextGroup(Request $request)
    {
        ImageText::where('id', (int)$request->input('id'))
            ->update(['gid' => (int)$request->input('gid')]);
        return $this->success();
    }

    public function addGroup(Request $request, ImageTextGroup $imageTextGroup)
    {
        $name = trim($request->input('name'));
        $imageTextGroup->name = $name;
        $imageTextGroup->save();
        return $this->success();
    }

    public function deleteGroup(Request $request, ImageTextGroup $imageTextGroup)
    {
        $id = (int)$request->get('id');
        if($id <= 0){
            return $this->error();
        }
        $imageTextGroup::destroy($id);
        return $this->success();
    }

    public function groupLists(Request $request, ImageTextGroup $imageTextGroup)
    {
        $pageSize = $request->get('pageSize', 10);

        $list = $imageTextGroup->orderBy('id', 'desc')->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }



}